package demo3;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

class Order<T extends Serializable> {
    int orderId;
    T user;
    List<String> products;

    public Order(int orderId, T user, List<String> products) {
        this.orderId = orderId;
        this.user = user;
        this.products = products;
    }
}

class UserInfo implements Serializable {
    String city;

    public UserInfo(String city) {
        this.city = city;
    }

    public String getCity() {
        return city;
    }
}

public class OrderProcessingSystem {
    public static <T extends Serializable> List<Order<T>> filterOrdersByCity(List<Order<T>> orders, String city) {
        return orders.stream()
                .filter(order -> ((UserInfo) order.user).getCity().equals(city))
                .collect(Collectors.toList());
    }

    public static <T extends Serializable> List<Order<T>> mergeOrders(List<List<Order<T>>> orderLists) {
        return orderLists.stream()
                .flatMap(List::stream)
                .distinct()
                .collect(Collectors.toList());
    }

    public static <T extends Serializable> Map<String, Integer> countOrdersByCity(List<Order<T>> orders) {
        Map<String, Integer> cityOrderCount = new HashMap<>();
        for (Order<T> order : orders) {
            String city = ((UserInfo) order.user).getCity();
            cityOrderCount.merge(city, 1, Integer::sum);
        }
        return cityOrderCount;
    }

    public static void main(String[] args) {
        List<Order<UserInfo>> orders = new ArrayList<>();
        orders.add(new Order<>(1, new UserInfo("New York"), List.of("Product1")));
        orders.add(new Order<>(2, new UserInfo("Los Angeles"), List.of("Product2")));
        orders.add(new Order<>(3, new UserInfo("New York"), List.of("Product3")));
        List<Order<UserInfo>> filteredOrders = filterOrdersByCity(orders, "New York");
        System.out.println("纽约的订单: " + filteredOrders);
        List<List<Order<UserInfo>>> orderLists = List.of(orders, orders);
        List<Order<UserInfo>> mergedOrders = mergeOrders(orderLists);
        System.out.println("合并后的订单: " + mergedOrders);
        Map<String, Integer> cityOrderCount = countOrdersByCity(orders);
        System.out.println("各城市的订单数量: " + cityOrderCount);
    }
}
